package com.hnevc.mytrafficdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hnevc.mytrafficdemo.R;
import com.hnevc.mytrafficdemo.adapter.AntiCarDetailAdapter;
import com.hnevc.mytrafficdemo.adapter.AntiCarListAdapter;
import com.hnevc.mytrafficdemo.entity.AntiTrafficDetail;
import com.hnevc.mytrafficdemo.entity.Car;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class AntiTrafficDetailActivity extends AppCompatActivity {
    List<AntiTrafficDetail> mAantiList;
    List<Car> mCarList;
    SimpleDateFormat sdf;
    String currentCarNum;
    Car car;
    List<AntiTrafficDetail> showAntiDetailList;
    ListView mCarListView,mAntiDetailListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anti_traffic_detail);

        mCarListView = (ListView) findViewById(R.id.id_lv_cars);
        mAntiDetailListView = (ListView) findViewById(R.id.id_lv_anti_details);

        mAntiDetailListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AntiTrafficDetail antiTrafficDetail = showAntiDetailList.get(i);
                Intent intent = new Intent(AntiTrafficDetailActivity.this
                        ,AntiTrafficMonitorActivity.class);
                startActivity(intent);
            }
        });
        sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        initData();
        currentCarNum = getIntent().getStringExtra("carnum");
        car = new Car();
        car.setCardNum(currentCarNum);
        initView(currentCarNum);
    }

    private void initView(String currentCarNum) {
        //显示
        showAntiDetailList.clear();
        for(int i=0;i<mAantiList.size();i++){
            if(mAantiList.get(i).getCarNumber().equals(currentCarNum)){
                AntiTrafficDetail antidetail = mAantiList.get(i);
                car.setWeizhangcishu(car.getWeizhangcishu()+1);
                car.setFakuang(car.getFakuang()+antidetail.getFakuang());
                car.setKoufen(car.getKoufen()+antidetail.getKoufen());
                showAntiDetailList.add(antidetail);
            }
        }
        if(!exist(car,mCarList)) {
            mCarList.add(car);
        }
        AntiCarDetailAdapter antiDetailAdpter = new AntiCarDetailAdapter(this,showAntiDetailList);
        mAntiDetailListView.setAdapter(antiDetailAdpter);

        AntiCarListAdapter  antiCarAdapter = new AntiCarListAdapter(this,mCarList);
        mCarListView.setAdapter(antiCarAdapter);



    }

    private boolean exist(Car car, List<Car> showCarList) {
        for (int i=0; i<showCarList.size();i++){
            if(showCarList.get(i).getCardNum().equals(car.getCardNum())){
                return true;
            }
        }
        return false;
    }

    private void initData() {
        mCarList = new ArrayList<>();
        showAntiDetailList = new ArrayList<>();
        try {
            mAantiList = new ArrayList<>();

            AntiTrafficDetail anitDetail = new AntiTrafficDetail();
            anitDetail.setCarNumber("鲁A10001");
            anitDetail.setAntiMessage("驾驶机动车在高速公路，城市快速路以外的道路上不按规定车道行驶");
            anitDetail.setAntiTime(sdf.parse("2017-04-02 17:55:00"));
            anitDetail.setKoufen(0);
            anitDetail.setFakuang(100);
            anitDetail.setAntiRoad("学院路");
            anitDetail.setState("未处理");
            mAantiList.add(anitDetail);
            anitDetail = new AntiTrafficDetail();
            anitDetail.setCarNumber("鲁A10001");
            anitDetail.setAntiMessage("驾驶机动车在高速公路，城市快速路以外的道路上不按规定车道行驶");
            anitDetail.setAntiTime(sdf.parse("2017-04-02 18:23:00"));
            anitDetail.setKoufen(1);
            anitDetail.setFakuang(100);
            anitDetail.setAntiRoad("医院路");
            anitDetail.setState("未处理");
            mAantiList.add(anitDetail);
            anitDetail = new AntiTrafficDetail();
            anitDetail.setCarNumber("鲁A10002");
            anitDetail.setAntiMessage("驾驶机动车在高速公路，城市快速路以外的道路上不按规定车道行驶");
            anitDetail.setAntiTime(sdf.parse("2017-01-02 18:23:00"));
            anitDetail.setKoufen(2);
            anitDetail.setFakuang(300);
            anitDetail.setAntiRoad("高速路");
            anitDetail.setState("未处理");
            mAantiList.add(anitDetail);
            anitDetail = new AntiTrafficDetail();
            anitDetail.setCarNumber("鲁A10002");
            anitDetail.setAntiMessage("闯红灯");
            anitDetail.setAntiTime(sdf.parse("2017-01-01 19:23:00"));
            anitDetail.setKoufen(6);
            anitDetail.setFakuang(600);
            anitDetail.setAntiRoad("学院路");
            anitDetail.setState("未处理");
            mAantiList.add(anitDetail);

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
